<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form ref="filterFormRef" :model="filterForm" :inline="true">
          <el-form-item prop="allDate" label="执行时间">
            <el-date-picker
              v-model="allDate"
              popper-class="popperClass"
              :append-to-body="false"
              style="max-width: 80%;"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeTime"
            />
          </el-form-item>
          <el-form-item label="服务名称" prop="appName">
            <el-input v-model.trim="filterForm.appName" maxlength="30" clearable
                      placeholder="请输入内容" class="filter-item limit" @keyup.enter.native="handleFilter" />
          </el-form-item>
          <el-form-item label="日志名称" prop="logName">
            <el-input v-model.trim="filterForm.logName" maxlength="30" clearable
                      placeholder="请输入内容" class="filter-item limit" @keyup.enter.native="handleFilter" />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset"> 重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <!-- <el-button v-permission="['OPERATE_LOG_DEL_BUTTON']" type="danger" icon="el-icon-delete" @click="onEmpty">清空日志</el-button> -->
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          :data="tableData"
          style="width: 100%"
          :header-cell-style="headerCellStyle"
          border
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="table_index"
          />
          <el-table-column
            v-if="columns[0].show"
            key="account"
            prop="account"
            label="账号"
            min-width="90"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[1].show"
            key="realName"
            prop="realName"
            label="用户姓名"
            min-width="100"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[2].show"
            key="appName"
            prop="appName"
            label="服务名称"
            min-width="100"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[3].show"
            key="logModule"
            prop="logModule"
            label="操作模块"
            min-width="100"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[4].show"
            key="logContent"
            prop="logContent"
            label="日志名称"
            min-width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[5].show"
            key="requestUrl"
            prop="requestUrl"
            label="请求地址"
            min-width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[6].show"
            key="clientIp"
            prop="clientIp"
            label="客户端IP"
            min-width="110"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[7].show"
            key="serverIp"
            prop="serverIp"
            label="服务端IP"
            min-width="110"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[8].show"
            key="clientBrowser"
            prop="clientBrowser"
            label="浏览器"
            min-width="90"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[9].show"
            key="clientOs"
            prop="clientOs"
            label="操作系统"
            min-width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[10].show"
            key="status"
            prop="status"
            label="状态"
            min-width="90"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.status == 0 ? '成功': scope.row.status == 1 ? '逻辑失败': '异常失败' }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[11].show"
            key="failReason"
            prop="failReason"
            label="失败原因"
            min-width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[12].show"
            key="createTime"
            prop="createTime"
            label="创建时间"
            width="170"
            :show-overflow-tooltip="true"
          />
          <el-table-column :key="Math.random()" fixed="right" label="操作"
                           class-name="small-padding fixed-width" width="84" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleDetail(scope.$index, scope.row)"
              >详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="totalRows > 0"
          :total="totalRows"
          :page.sync="filterForm.pageNo"
          :limit.sync="filterForm.pageSize"
          @pagination="getList"
        />
      </div>
      <dtDialog
        title="日志详情"
        :visible.sync="dialogVisible"
        :footer-slot="true"
      >
        <el-form
          slot="content"
          :model="form"
          label-position="right"
          :label-width="log.logMode==2&&log.annex? '174px':'72px'"
        >
          <el-form-item label="账号">
            <el-input v-model.trim="log.account" disabled />
          </el-form-item>
          <el-form-item label="用户姓名">
            <el-input v-model.trim="log.realName" disabled />
          </el-form-item>
          <el-form-item label="服务名称">
            <el-input v-model.trim="log.appName" disabled />
          </el-form-item>
          <el-form-item label="操作模块">
            <el-input v-model.trim="log.logModule" disabled />
          </el-form-item>
          <el-form-item label="日志名称">
            <el-input v-model.trim="log.logContent" disabled />
          </el-form-item>
          <el-form-item label="请求地址">
            <el-input v-model.trim="log.requestUrl" disabled />
          </el-form-item>
          <el-form-item label="客户端IP">
            <el-input v-model.trim="log.clientIp" disabled />
          </el-form-item>
          <el-form-item label="服务端IP">
            <el-input v-model.trim="log.serverIp" disabled />
          </el-form-item>
          <el-form-item label="浏览器">
            <el-input v-model.trim="log.clientBrowser" disabled />
          </el-form-item>
          <el-form-item label="操作系统">
            <el-input v-model.trim="log.clientOs" disabled />
          </el-form-item>
          <el-form-item label="状态">
            <el-input v-model.trim="log.status" disabled />
          </el-form-item>
          <el-form-item label="失败原因">
            <el-input v-model.trim="log.failReason" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model.trim="log.createTime" disabled />
          </el-form-item>
          <el-form-item label="请求方式">
            <el-input v-model.trim="log.httpMethod" disabled />
          </el-form-item>
          <el-form-item label="请求参数">
            <el-input v-model.trim="log.requestParams" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
          <el-form-item label="返回结果">
            <el-input v-model.trim="log.requestResult" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
          <el-form-item v-if="log.logMode==2&&log.annex" label="导入执行结果信息">
            <el-input v-model.trim="log.annex.tip" disabled />
          </el-form-item>
          <el-form-item v-if="log.logMode==2&&log.annex" label="失败的明细文件的下载url">
            <el-link :underline="false" @click="clickUrl"> {{ log.annex.fileName }}</el-link>
            <!-- <el-input id="fileNameId" v-model="log.annex.fileName" @click.native="clickUrl"> -->
            <!-- <el-button type="text" @click="clickUrl">{{ log.annex.fileName }}</el-button> -->
            <!-- </el-input> -->
          </el-form-item>
          <!-- <el-form-item v-if="log.logMode==2&&log.annex" label="失败明细文件名">
            <el-input v-model.trim="log.annex.fileName" :rows="5" type="textarea" disabled />
          </el-form-item> -->
          <el-form-item v-if="log.logMode==2&&log.annex" label="导入成功名单">
            <el-input v-model.trim="log.annex.successList" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
          <el-form-item v-if="log.logMode==2&&log.annex" label="导入失败名单">
            <!-- <div style="white-space: pre-line">
              <div style="white-space: pre-line">{{ log.annex.failMap }}</div>
            </div> -->
            <el-input v-model.trim="log.annex.failMap" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
          <el-form-item v-if="log.logMode==2&&log.annex" label="备注">
            <el-input v-model.trim="log.annex.remark" :rows="5" type="textarea"
                      disabled class="limit" />
          </el-form-item>
        </el-form>
      </dtDialog>
    </div>
  </div>
</template>

<script>
import
  systemApi
from '@/api/investment/system'

import { default as request, cloud } from '@/framework/utils/request'
import { getLogList, getLogDetail, deleteLog } from '@/framework/api/log'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'

export default {
  name: 'OperateLog',
  mixins: [dialogColumn],
  data() {
    return {
      allDate: '',
      filterForm: {
        beginDate: '',
        endDate: '',
        logName: '',
        pageNo: 1,
        pageSize: 10,
        appName: ''
      },
      tableData: [],
      totalRows: 0,
      totalPage: 0,
      dialogVisible: false,
      log: {
        annex: {
          successList: ''
        }
      },
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      // 列信息
      columns: [
        { prop: 'account', label: '账号', show: true },
        { prop: 'realName', label: '用户姓名', show: true },
        { prop: 'appName', label: '服务名称', show: true },
        { prop: 'logModule', label: '操作模块', show: true },
        { prop: 'logContent', label: '日志名称', show: true },
        { prop: 'requestUrl', label: '请求地址', show: true },
        { prop: 'clientIp', label: '客户端IP', show: true },
        { prop: 'serverIp', label: '服务端IP', show: true },
        { prop: 'clientBrowser', label: '浏览器', show: true },
        { prop: 'clientOs', label: '操作系统', show: true },
        { prop: 'status', label: '状态', show: true },
        { prop: 'failReason', label: '失败原因', show: true },
        { prop: 'createTime', label: '创建时间', show: true }
      ],
      form: {},
      options: {}
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    clickUrl() {
      request({
        url: cloud.dqbasic + this.log.annex.errorDownloadPath,
        method: 'get',
        responseType: 'arraybuffer'
      }).then((res) => {
        const blob = new Blob([res.data], {
          type: 'application/vnd.ms-excel'
        })
        const elink = document.createElement('a')
        elink.download = this.log.annex.fileName // 下载文件名称
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        document.body.removeChild(elink) // 下载完成移除元素
        window.URL.revokeObjectURL(elink) // 释放掉blob对象
      })
    },
    handleReset() {
      this.$refs.filterFormRef.resetFields()
      this.allDate = ''
      this.handleFilter()
    },
    changeTime(val) {
      if (!val) {
        this.allDate = []
      } else {
        this.allDate = val
      }
    },
    handleFilter() {
      this.filterForm.pageNo = 1
      this.getList()
    },
    onExport() {},
    onEmpty() {
      this.$confirm('此操作将清空日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((_) => {
        const params = {
          beginDate: this.filterForm.beginDate,
          endDate: this.filterForm.endDate,
          appName: this.filterForm.appName
        }
        if (this.allDate.length > 0) {
          params.beginDate = this.allDate[0]
          params.endDate = this.allDate[1]
        }
        deleteLog(params).then((resp) => {
          const type = resp.success ? 'success' : 'error'
          this.$dtMessage({
            type,
            message: `${resp.message}`
          })
          this.$refs.filterFormRef.resetFields()
          this.allDate = ''
          this.getList()
        })
      })
    },
    getList() {
      if (this.allDate) {
        this.filterForm.searchBeginTime = this.allDate[0]
        this.filterForm.searchEndTime = this.allDate[1]
      } else {
        this.filterForm.searchBeginTime = ''
        this.filterForm.searchEndTime = ''
      }
      systemApi.getSysLog(this.filterForm).then((resp) => {
        this.tableData = [...resp.data.rows]
        this.totalRows = resp.data.totalRows
        this.totalPage = resp.data.totalPage
      })
    },
    handleDetail(Index, row) {
      this.dialogVisible = true
      const params = {
        logId: row.logId
      }
      systemApi.getSysDetail(params).then((resp) => {
        this.log = { ...resp.data }
        if (this.log.logMode == 2) {
          if (this.log.annex) {
            if (this.log.annex.successList && this.log.annex.successList.length > 0) {
              this.log.annex.successList = this.log.annex.successList.join(',')
            }

            let result = ''
            const keys = Object.keys(this.log.annex.failMap); const values = Object.values(this.log.annex.failMap)
            keys.forEach((item, index) => {
              result = result ? `${result}
${item}:${values[index]}` : `${item}:${values[index]}`
            })
            this.log.annex.failMap = result
          }
        }
        this.log.status = this.log.status == 0 ? '成功' : this.log.status == 1 ? '逻辑失败' : '异常失败'
      })
    },
    table_index(index) {
      return (this.filterForm.pageNo - 1) * this.filterForm.pageSize + index + 1
    }
  }
}
</script>
<style lang="scss" scoped>
    .log{
    }
    .el-table .cell {
      white-space: nowrap;
    }
    .dialog-footer{
      text-align: center;
    }
    .table-header{
      background-color: chartreuse;
    }
    ::v-deep .popperClass.el-date-picker {
  width: 220px;
  height: 200px;
  &__header-label {
    font-size:14px;
  }
  .el-picker-panel__content {
    width: 220px;
    height: 200px;
    margin-left: 0;
  }
  .el-year-tabel td {
    padding: 5px 0;
    .cell {
      width: 32px;
      height: 32px;
    }
  }
}
</style>
